package process

import (
	"chat/common/msg"
	"chat/utils"
	"encoding/json"
	"fmt"
	"net"
)

type SmsProcess struct {
}

func (this *SmsProcess) SendGroupMes(mes *msg.Message) {
	// 遍历 onlineUsers 转发消息
	var smsMes msg.SmsMes
	err := json.Unmarshal([]byte(mes.Data), &smsMes)
	if err != nil {
		fmt.Println("SendGroupMes json.Unmarshal error ", err)
		return
	}

	data, err := json.Marshal(mes)
	if err != nil {
		fmt.Println("SendGroupMes json.Marshal error ", err)
		return
	}

	for id, userProcess := range userManager.onlineUsers {
		// 不发送给自己
		if id == smsMes.UserId {
			continue
		}
		this.SendMesToEach(data, userProcess.Conn)
	}
}

func (this *SmsProcess) SendMesToEach(data []byte, conn net.Conn) {
	tf := &utils.Transfer{
		Conn: conn,
	}
	err := tf.WritePkg(data)
	if err != nil {
		fmt.Println("SendMesToEach WritePkg error ", err)
	}
}
